package com.letv.app.ljodfh.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.jiongbull.jlog.JLog;
import com.letv.app.ljodfh.base.BaseFragment;

/**
 * Created by liuyu on 2017/9/3.
 */

public class BasePagerFragment extends BaseFragment {

    protected boolean hasInited;
    private int showCount;

    protected boolean isFront;//是否在前面

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews(view, savedInstanceState);
        hasInited = true;
//        JLog.e("rrrs hase inited count=  "+showCount+"  "+getUserVisibleHint()+" " +isThisShowing+"   "+ this);
        if (isParentShowing && isThisShowing) {
            if (onFragmentShow(showCount == 0, false)) {
                showCount++;
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
//        JLog.e("ddac is visible user " + isVisibleToUser + "  count=" + showCount + "   "
//                + getUserVisibleHint() + "   hasInited=" + hasInited + "   isParentSHow="+isParentShowing+"  " + this);
        if (isParentShowing && hasInited) {
            if (getUserVisibleHint()) {
                isFront = true;
                isThisShowing = true;
                if (onFragmentShow(showCount == 0, false)) {
                    showCount++;
                }
            } else {
                isFront = false;
                onFragmentUnShowed(false);
                isThisShowing = false;
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (isParentShowing) {
            isFront = !hidden;
            if (hasInited) {
                if (isFront) {
                    isThisShowing = true;
                    onFragmentShow(showCount == 0, false);
                } else {
                    onFragmentUnShowed(false);
                    isThisShowing = false;
                }
            }
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
//        JLog.e("rrrs  count="+showCount +"  "+getUserVisibleHint()+"   "+this);
    }

    @Override
    public void onDestroyView() {
        hasInited = false;
        showCount = 0;
//        JLog.e("rrrs  count="+showCount +"  "+getUserVisibleHint()+"   "+this);
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isParentShowing && isFront && showCount > 0) {
            if (onFragmentShow(false, true)) {
                showCount++;
            }
        }
    }

    @Override
    public void onPause() {
//        JLog.e("okh  "+this+"\n   hasinit="+hasInited+"  isFront="+isFront+"  isParentShowing="+isParentShowing);
        if (hasInited && isFront && isParentShowing) {
            onFragmentUnShowed(true);
        }
        super.onPause();
    }

    @Override
    public void setParentShowing(boolean parentShowing) {
        super.setParentShowing(parentShowing);
        if (hasInited && isFront) {
//            JLog.e("ddac  will call fragment is show " + parentShowing + "   " + this);
            if (parentShowing) {
                isThisShowing = true;
                onFragmentShow(isFirst(), false);
            } else {
                isThisShowing = false;
                onFragmentUnShowed(true);
            }
        }
    }

    protected boolean isFirst() {
        return showCount == 0;
    }

    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
    }

    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
//        JLog.e("rrrs is first  " + isFirst + "   " + this);
        return true;
    }

    protected void onFragmentUnShowed(boolean isCallFromPause) {
//        JLog.e(" rrrs  " + this);
    }

    private void delayedLog() {
//        JLog.e("rrrs delayed log count=  "+showCount+"  "+getUserVisibleHint()+" " +this );
    }

    public int getShowCount() {
        return showCount;
    }
}
